package Simulation;

import mathwork.Mathwork;
import fxana.FxRawDataSet;
import fxana.RawDataCell;

public class SimulatorLSMDouble extends Simulator {
	public static int opt_numberofday=27;
	public static int opt_losscutval=24;
	public static int opt_ref_numberofday=25;

	public SimulatorLSMDouble(){
		super();
	}
	
	@Override
	protected void ParameterOptimize(FxRawDataSet dataset,
			EvaluatorParaFlag pflag) {
		int n=0,on=0;
		int l=0,ol=0;
		int r=0,or=0;
		Double MaxMerit=0.0;
		
		for (r=5;r<30;r++){
			for(n=5;n<50;n++){
				for(l=5;l<35;l++){
					EvaluatorParaFlag  spflag=new EvaluatorParaFlag ();
					spflag.SetMFlag(n);
					spflag.SetLFlag(l);
					spflag.SetRFlag(r);
				
					ConditionLSMDouble seed=new ConditionLSMDouble();
					SimulationSystem eval=new SimulationSystem(dataset,spflag,seed);
					eval.output();
					if(eval.GetTotalMerit().compareTo(MaxMerit)>0){
						on=n;
						ol=l;
						or=r;
						MaxMerit=eval.GetTotalMerit();
					}
					System.out.println("Temp MaxMerit:"+Mathwork.Roundup(MaxMerit,2)+" "
											+ "N:"+on+" "
											+ "L:"+ol+" "
											+ "R:"+or);
				}
			}
		}
		
		System.out.println("MaxMerit:"+Mathwork.Roundup(MaxMerit,2)+" "+ "is following:");
		EvaluatorParaFlag  spflag=new EvaluatorParaFlag ();
		spflag.SetMFlag(on);
		spflag.SetLFlag(ol);
		spflag.SetRFlag(or);
		
		Condition seed;
		seed=new ConditionLSMDouble();
		SimulationSystem eval=new SimulationSystem(dataset,spflag,seed);
		eval.output();
	}

	@Override
	protected void OneTest(FxRawDataSet dataset, EvaluatorParaFlag pflag) {
		ConditionLSMDouble seed=new ConditionLSMDouble();
		SimulationSystem eval=new SimulationSystem(dataset,pflag,seed);
		eval.output();
	}

	@Override
	protected void OutParameter(FxRawDataSet dataset, EvaluatorParaFlag pflag) {
		RawDataCell today=dataset.GetRawDataBuffer().lastElement();
		Double sellval=0.0;
		Double buyval=0.0;
		Double buylosscut=0.0;
		Double selllostcut=0.0;
		
		ConditionLSMDouble seed=null;
		EvaluatorParaFlag  spflag=new EvaluatorParaFlag ();
		
		seed=new ConditionLSMDouble();
		spflag.SetMFlag(SimulatorLSMDouble.opt_numberofday);
		spflag.SetLFlag(SimulatorLSMDouble.opt_losscutval);
		spflag.SetRFlag(SimulatorLSMDouble.opt_ref_numberofday);
		
		//Mathwork.ifplot=true;
		RawDataCell est=seed.GetEstimatedValue(dataset, today,spflag);
		//Mathwork.ifplot=false;
		if(est!=null){
			sellval=Mathwork.Roundup(est.GetHighValue(),2);
			selllostcut=Mathwork.Roundup(sellval+SimulatorLSMDouble.opt_losscutval/10.0,2);
			buyval=Mathwork.Roundup(est.GetLowValue(),2);
			buylosscut=Mathwork.Roundup(buyval-SimulatorLSMDouble.opt_losscutval/10.0,2);
		}else{
			System.out.println("Today can not be estimated!");
			sellval=selllostcut=buyval=buylosscut=0.0;
		}
		
		today.out();
		if(pflag.IsEFlagSet()){
			System.out.println("N: "+SimulatorLSMDouble.opt_numberofday+" "+
								  "L: "+SimulatorLSMDouble.opt_losscutval/10.0+
								  "R: "+SimulatorLSMDouble.opt_ref_numberofday);
		}
		System.out.println(today.GetFSRecDate()+" "+"Sellval: "+sellval+" "+"Buyval: "+buyval+" "+"SellLosscut: " + selllostcut+" "+"Buylosscut: "+buylosscut);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		SimulatorLSMDouble sim=new SimulatorLSMDouble();
		sim.DoSimulator(args);
	}

}
